<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '角色添加' : '角色编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="cancel"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="角色名称"
          ><el-input v-model="form.rolename"></el-input>
        </el-form-item>
        <el-form-item label="角色权限">
          <el-tree
            :data="menuList"
            show-checkbox
            node-key="id"
            ref="tree"
            :props="{
              children: 'children',
              label: 'title',
            }"
          >
          </el-tree>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="obj.isAdd">添 加</el-button>
        <el-button type="primary" @click="edit" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { roleAdd,roleInfo,roleEdit } from "../../../request/api";
import { successAlert, warningAlert } from "../../../utils/alert";
export default {
  props: ["obj"],
  methods: {
    ...mapActions({
      asyncMenuList: "menu/asyncChangeList",
      asyncRoleList:"role/asyncChangeList"
    }),
    //重置对话框内容
    reset() {
      //清空输入框
      this.form = {
        rolename: "",
        menus: "",
        status: 1,
      },
        //清空树形控件
        this.$refs.tree.setCheckedKeys([]);
    },
    cancel() {
      this.obj.isTrue = false;
      this.reset();
    },
    add() {
      //对角色权限的操作进行处理，并转为字符串对象
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      roleAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          //重新请求列表数据
          this.asyncRoleList()
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getInfo(id){
      roleInfo({id:id}).then(res=>{
        //渲染表单内容
        this.form={
          ...res.data.list,
          menus:JSON.parse(res.data.list.menus),
          id:id
        }
        //渲染树状控件内容
        this.$refs.tree.setCheckedKeys(this.form.menus);
      })
      console.log(this.form);
    },
    edit(){
      //在添加前也要获取到树形控件选中的内容
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys()),
      roleEdit(this.form).then(res=>{
        if(res.data.code==200){
          successAlert(res.data.msg)
          this.cancel();
          this.asyncRoleList()
        }else{
          warningAlert(res.data.msg)
        }
      })
    }
  },
  computed: {
    ...mapGetters({
      menuList: "menu/list",
      roleList:"role/list"
    }),
  },
  data() {
    return {
      form: {
        rolename: "",
        menus: "",
        status: 1,
      },
    };
  },
  mounted() {
    //页面一进来就请求menu的列表数据
    this.asyncMenuList();
  },
};
</script>

<style>
</style>